<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Tasks;

use Swoft\App;
// use Swoft\Bean\Annotation\Inject;
// use Swoft\HttpClient\Client;
// use Swoft\Rpc\Client\Bean\Annotation\Reference;
use Swoft\Task\Bean\Annotation\Scheduled;
use Swoft\Task\Bean\Annotation\Task;

/**
 * Class MerchantTask - define some tasks
 *
 * @Task("merchant")
 * @package App\Tasks
 */
class MerchantTask
{
    /**
     * A work task
     * do something
     *
     * @param string $p1
     * @param string $p2
     *
     * @return string
     */
    public function work(string $p1, string $p2)
    {
        App::profileStart('co');
        App::trace('trace');
        App::info('info');
        App::pushlog('key', 'stelin');
        App::profileEnd('co');

        return 'o, task completed';
    }

    /**
     * A cronTab task
     * 3-5 seconds per minute 每分钟第3-5秒执行
     *
     * @Scheduled(cron="3-5 * * * * *")
     */
    public function cronTask()
    {
        echo time() . "第3-5秒执行\n";

        return 'cron';
    }

    /**
     * 订单主动查询任务
     * 每秒执行
     *
     * @Scheduled(cron="* * * * * *")
     */
    public function refreshOrderTask()
    {
        echo time() . "第3-5秒执行\n";

        return 'cron';
    }

    /**
     * Send Order async task
     * 订单推送
     *
     * @param string $table
     * @param string $id
     *
     * @return string
     */
    public function sendOrderAsync(string $table, string $id)
    {
        App::info('sendOrder='.$table.'@'.$id);
        sleep(2);
        App::info('sendOrder='.$table.'@'.$id.' result=ok');

        return sprintf('deliverCo-%s-%s', $table, $id);
    }

    /**
     * callback Order async task
     * 订单回调
     *
     * @param string $backData
     * @param string $backUrl
     *
     * @return string
     */
    public function callbackOrderAsync(string $backUrl, string $backData)
    {
        App::info('callbackOrder='.$backUrl.'@'.$backData);
        sleep(2);
        App::info('callbackOrder='.$backUrl.'@'.$backData.' result=ok');

        return sprintf('deliverCo-%s-%s', $backUrl, $backData);
    }
}
